<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Models\Goods;
use App\Http\Requests\GoodsRequest;
use App\Services\GoodsServices;

class GoodsController extends Controller
{
    public function goods_info(GoodsRequest $request,Goods $goods){
        $data = $goods->where('id',$request->goods_id)
            ->first();
        return $this->success($data);
    }

    public function buy_goods(GoodsRequest $request,GoodsServices $goodsServices){
        try{
            $result = $goodsServices->buy_goods($request->goods_id,auth('api') -> user()->id);
            if($result){
                return $this->success(['order_id'=>$result]);
            }
            throw new \Exception('保存失败');
        }catch (\Exception $e){
            return $this->fail($e -> getMessage());
        }
    }

    public function pay_order(GoodsRequest $request,GoodsServices $goodsServices){
        $result = $goodsServices->pay_order($request->order_id,auth('api') -> user()->id,auth('api') -> user()->wx_openid);
        return $this->success($result);
    }
}
